package com.yjj.im.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.yjj.im.R;
import com.yjj.im.Utils.AFinalHttp;
import com.yjj.im.Utils.Constant;
import com.yjj.im.Utils.DeviceInfoUtils;
import com.yjj.im.Utils.GsonFomater;
import com.yjj.im.bean.LoginDataBean;
import com.yjj.im.bean.LoginResultBean;
import com.yjj.im.bean.ResultBean;

import java.util.HashMap;
import java.util.Map;

public class SplanshActivity extends Activity implements OnClickListener {

    private EditText clientId;
    private EditText userName;
    private EditText password;
    Button loginButton;
    private static final String baseUrl ="http://server.shier2020.cn/";
    public static String loginUserId;
    private String remember = "0";
    private Button rememberButton;
    private String loginUserName;
    private String loginpassword;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        loginUserName = (String)Constant.getValue(SplanshActivity.this,Constant.currentlogiUserName);
        loginpassword = (String)Constant.getValue(SplanshActivity.this,Constant.currentLoginPassword);
        remember = (String)Constant.getValue(SplanshActivity.this,Constant.currentRemember);
        String deviceInfo = DeviceInfoUtils.getDeviceAllInfo(this);
        clientId = (EditText) findViewById(R.id.et_clientId);
        userName = (EditText)findViewById(R.id.et_user_name);
        password = (EditText)findViewById(R.id.et_pass_word);
        loginButton = (Button) this.findViewById(R.id.login);
        rememberButton = (Button) this.findViewById(R.id.btn_remember);
        if("1".equals(remember)){
            rememberButton.setBackgroundResource(R.mipmap.auth_follow_cb_chd);
        }
        if(loginUserName != null){
            userName.setText(loginUserName);
        }
        if(loginpassword != null){
            password.setText(loginpassword);
        }
        loginButton.setOnClickListener(this);
        rememberButton.setOnClickListener(this);
//        String a = Constant.decode("");
//        String b = Constant.encode("");
//        String c = Constant.toAscii("");
//        String d = Constant.toStringHex1("");
//        Log.e("baosha","a:["+a+"]");
//        Log.e("baosha","b:["+b+"]");
//        Log.e("baosha","c:["+c+"]");
//        Log.e("baosha","d:["+d+"]");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:
//                forwordPage();
                login(baseUrl+"jzzq!mqlogin.do",userName.getText().toString(),password.getText().toString());
                break;
            case R.id.btn_remember:
                if(remember.equals("0")||remember.equals("")){
                    rememberButton.setBackgroundResource(R.mipmap.auth_follow_cb_chd);
                    remember = "1";
                }else{
                    rememberButton.setBackgroundResource(R.mipmap.auth_follow_cb_unc);
                    remember = "0";
                }
                break;
        }

    }

    private void forwordPage(){
        Intent intent = new Intent(this, MessageActivity.class);
        intent.putExtra("clientId", clientId.getText().toString());
        intent.putExtra("userName", userName.getText().toString());
        intent.putExtra("password", password.getText().toString());
        startActivity(intent);
    }
    private void login(String url,String userName,String passWord){

        Map<String, String> params = new HashMap<>();
        params.put("userName",userName);
        params.put("password",passWord);
        params.put("view","json");

        AFinalHttp.requestForPost(url, params, null,new AFinalHttp.CallBack(this) {

            @Override
            protected void success(Object data) {
                if(data == null){
                    return;
                }
                Log.e("",""+data.toString());
                LoginResultBean resultBean = GsonFomater.format(data.toString(),LoginResultBean.class);
                if(!resultBean.success ){
                    Toast.makeText(SplanshActivity.this, resultBean.msg, Toast.LENGTH_LONG).show();
                }else{
                    Constant.cleanSharedPreferences(SplanshActivity.this);
                    LoginDataBean loginDataBean =  resultBean.result.get(0);
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(Constant.clientname,loginDataBean.clientname);
                    map.put(Constant.serverUrl, loginDataBean.curl);
                    map.put(Constant.mqttTopic, loginDataBean.ctopic);
                    map.put(Constant.mqttServer, loginDataBean.chost);
                    map.put(Constant.mqttServerPort, loginDataBean.cport);
                    map.put(Constant.mqttUser, loginDataBean.cuser);
                    map.put(Constant.mqttpassword, loginDataBean.cpw);
                    map.put(Constant.currentUsername, loginDataBean.name);
                    map.put(Constant.currentUserId, loginDataBean.id);
                    map.put(Constant.currentUserId, loginDataBean.id);
                    map.put(Constant.currentisAuto, loginDataBean.isauto);
                    map.put(Constant.currentLoginPassword, "");
                    if("1".equals(remember)){
                        map.put(Constant.currentLoginPassword, passWord);
                    }
                    map.put(Constant.currentlogiUserName, userName);
                    map.put(Constant.currentRemember, remember);

                    Constant.editSharedPreferences(map, SplanshActivity.this);
                    forwordPage();
                }
//                parsedata(data.toString(),idList,nameList);
//                if(nameList!= null && nameList.size() >0){
//
////                    Message msg = new Message();
////                    msg.what = 4;
////                    handler.sendMessage(msg);
//                }
//                else {
//                    Utils.toast("获取数据成功");
//                }
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg){
                Log.e("baoshann",strMsg);
            }
        });
    }



}